package org.lib.eshello.news.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.lib.eshello.news.dto.AddNewsDTO;
import org.lib.eshello.news.dto.FindByPageDTO;
import org.lib.eshello.news.entity.News;
import org.lib.eshello.news.service.NewsService;
import org.lib.eshello.news.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * News接口
 * @author ben
 * @date 2021-09-01 14:44:33 CST
 */
@RestController
@RequestMapping(value="/news")
@Slf4j
public class NewsController {
	
	@Autowired
	private NewsService newsService;
	
	@Autowired
	private HttpServletResponse httpServletResponse;
	
	/**
	 * 添加新闻
	 * @author ben
	 * @date 2021-09-01 14:07:23 CST
	 * @param dto
	 * @return
	 */
	@PostMapping(value="/add")
	public String addNews(@RequestBody AddNewsDTO dto) {
		log.info("添加News");
		return newsService.addNews(dto);
	}
	
	/**
	 * 获取所有（危险！非生产调试用）
	 * @author ben
	 * @date 2021-09-01 14:11:51 CST
	 * @return
	 */
	@GetMapping(value="/getAll")
	public List<News> getAll() {
		return newsService.getAll();
	}
	
	@GetMapping(value="/getById")
	public News getById(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}
		
		return newsService.getById(id);
	}
	
	// 普通查找
	
	/**
	 * 根据title查找
	 * 就是模糊查询
	 * @author ben
	 * @date 2021-09-01 15:45:45 CST
	 * @param title
	 * @return
	 */
	@GetMapping(value="/findByTitle")
	public List<News> findByTitle(@RequestParam String title) {
		return newsService.findByTitle(title);
	}
	
	/**
	 * findByTitleContaining
	 * 无效，TODO
	 * 使用ik中文分词器后，可以查询到结果了
	 * @author ben
	 * @date 2021-09-01 15:46:03 CST
	 * @param title
	 * @return
	 */
	@GetMapping(value="/findByTitleContaining")
	public List<News> findByTitleContaining(@RequestParam String title) {
		return newsService.findByTitleContaining(title);
	}
	
	// 分页查找
	
	/**
	 * 分页查找有效
	 * 异常：搜索的 keyWord 不存在时，也能找到数据——没有把keyWord作为一个整体，TODO
	 * @author ben
	 * @date 2021-09-01 16:17:18 CST
	 * @param dto
	 * @return
	 */
	@PostMapping(value="/findByTitleOrContentText")
	public PageVO<News> findByTitleOrContentText(@RequestBody FindByPageDTO dto) {
		log.info("dto={}", dto);
		return newsService.findByTitleOrContentText(dto);
	}
	
	// 其它操作
	
	/**
	 * 根据ID删除文档
	 * @author ben
	 * @date 2021-09-17 12:41:21 CST
	 * @param id
	 * @return
	 */
	@GetMapping(value="/deleteById")
	public Boolean deleteById(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			try {
				httpServletResponse.sendError(HttpStatus.BAD_REQUEST.value(), "请求参数错误");
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		return newsService.deleteById(id);
	}
	
}
